import pygame

from actor import WalkDir, DirWalkByAImage


class WalkSoldier(pygame.sprite.Sprite):
    hp = 100
    name = '某军'
    pos_x = 100
    pos_y = 350
    image_width = 50
    image_height = 100

    walk_action = None
    rect = ()

    def __init__(self, walk_path: str, pos_x, pos_y, name, hp):
        super(WalkSoldier, self).__init__()
        self.walk_action = DirWalkByAImage(walk_path, self.image_width, self.image_height, True)
        self.walk_action.set_dir(WalkDir.down)
        self.pos_x = pos_x
        self.pos_y = pos_y
        self.name = name
        self.hp = hp
        self.rect = pygame.Rect(self.pos_x - 5, self.pos_y - 10, self.image_width - 10, self.image_height - 20)

    # 0 下 1 左  2 右 3 上
    def run(self, key: int):
        if key == pygame.K_DOWN or key == pygame.K_s:
            self.walk_action.set_dir(WalkDir.down)
            self.pos_y += 10
        elif key == pygame.K_UP or key == pygame.K_w:
            self.walk_action.set_dir(WalkDir.up)
            self.pos_y += -10
        elif key == pygame.K_LEFT or key == pygame.K_a:
            self.walk_action.set_dir(WalkDir.left)
            self.pos_x += -10
        elif key == pygame.K_RIGHT or key == pygame.K_d:
            self.walk_action.set_dir(WalkDir.right)
            self.pos_x += 10

    def draw(self, surface: pygame.Surface):
        """
        绘制
        :param surface:
        :param x:
        :param y:
        :return:
        """
        current_image = self.walk_action.get_image()
        surface.blit(current_image, (self.pos_x, self.pos_y))
